package tingshu.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import tingshu.service.AlbumService;
import tingshu.util.Log;
import tingshu.view_object.AlbumResultView;
import tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 展示专辑列表
 * @author houdaiqin
 * @date 2022/07/17 18:25
 **/
@WebServlet("/studio/album/list.json")
public class ListJsonServlet extends HttpServlet {
    private final AlbumService albumService=new AlbumService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO currentUser=null;
        HttpSession session= req.getSession(false);
        if (session!=null){
            currentUser=(UserVO) session.getAttribute("currentUser");
        }
        if (currentUser==null){
            Log.println("用户未登录");
            return;
        }

        AlbumResultView albumResultView=albumService.getAlbumList(currentUser);
        ObjectMapper objectMapper = new ObjectMapper();

        //JSON序列化
        String json = objectMapper.writeValueAsString(albumResultView);
        // 响应
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        resp.getWriter().println(json);
    }
}